const router = require('koa-router')()
const sqlQuery = require('../db')
let moment = require('../util/format');
router.prefix('/socket')
// 获取历史信息
router.get('/history', async (ctx, next) => {
    try {
        let { uid, oid } = ctx.query;
        let sql = `SELECT * FROM (
            SELECT u.id,u.avatarUrl,m.content,DATE_FORMAT(m.createtime,'%Y-%m-%d %H:%i:%s') createtime,m.chose FROM messages m JOIN users u ON u.id=m.userid AND m.userid=? AND m.other_user_id=? AND m.state=1
            UNION
            SELECT u.id,u.avatarUrl,m.content,DATE_FORMAT(m.createtime,'%Y-%m-%d %H:%i:%s') createtime,m.chose FROM messages m JOIN users u ON u.id=m.userid AND m.userid=? AND m.other_user_id=? AND m.state=1
            ) message HAVING DATEDIFF(DATE_FORMAT(NOW(),'%Y-%m-%d'),DATE_FORMAT(createtime,'%Y-%m-%d'))>0 ORDER BY createtime ASC`;
        let result = await sqlQuery(sql, [uid, oid, oid, uid]);
        await Promise.all(result.map(async item => {
            if (item.id == uid) {
                item.select = "right"
            } else if (item.id == oid) {
                item.select = "left"
            }
        }))
        ctx.body = {
            state: 200,
            message: result
        }
    } catch (error) {
        next(error)
    }
})
// 获取好友发送信息
router.get('/init', async (ctx, next) => {
    try {
        let { uid, oid } = ctx.query;
        let sql = `SELECT * FROM (
            SELECT u.id,u.avatarUrl,m.content,DATE_FORMAT(m.createtime,'%Y-%m-%d %H:%i:%s') createtime,m.chose FROM messages m JOIN users u ON u.id=m.userid AND m.userid=? AND m.other_user_id=? 
            UNION
            SELECT u.id,u.avatarUrl,m.content,DATE_FORMAT(m.createtime,'%Y-%m-%d %H:%i:%s') createtime,m.chose FROM messages m JOIN users u ON u.id=m.userid AND m.userid=? AND m.other_user_id=? 
            ) message HAVING DATEDIFF(DATE_FORMAT(NOW(),'%Y-%m-%d'),DATE_FORMAT(createtime,'%Y-%m-%d'))=0 ORDER BY createtime ASC`;
        let result = await sqlQuery(sql, [uid, oid, oid, uid]);

        await Promise.all(result.map(async item => {
            if (item.id == uid) {
                item.select = "right"
            } else if (item.id == oid) {
                item.select = "left"
            }
            await sqlQuery('update messages set state=1 where userid=?', [item.id]);
        }))
        ctx.body = {
            state: 200,
            message: result
        }
    } catch (error) {
        next(error)
    }
})
// 发送信息
router.post('/send', async (ctx, next) => {
    try {
        let { uid, oid, message } = ctx.request.body;
        uid = parseInt(uid);
        oid = parseInt(oid);
        let params = [message, uid, oid, moment.formatTime(new Date())];
        await sqlQuery('insert into messages(content,userid,other_user_id,createtime) values(?,?,?,?)', params)
        ctx.body = "成功"
    } catch (error) {
        next(error)
    }
})
// 申请列表
router.get('/useritems', async (ctx, next) => {
    try {
        let { userid } = ctx.query;
        let sql = `SELECT users.id,users.avatarUrl,users.nickName,users.language comment FROM users JOIN
        (SELECT user_id FROM follow lf WHERE state=1 AND other_user_id=? AND NOT EXISTS(
        SELECT other_user_id FROM follow rf WHERE state=1 AND user_id=? AND lf.user_id=rf.other_user_id)) f ON users.id=f.user_id`;
        // 别人申请的我还未同意的
        let other = await sqlQuery(sql, [userid, userid]);
        sql = `SELECT users.id,users.avatarUrl,users.nickName,users.language comment FROM users JOIN
        (SELECT other_user_id FROM follow rf WHERE state=1 AND user_id=?  AND NOT EXISTS(
        SELECT user_id FROM follow lf WHERE state=1 AND other_user_id=? AND lf.user_id=rf.other_user_id)) f ON users.id=f.other_user_id`;
        // 我申请的别还未同意的
        let my = await sqlQuery(sql, [userid, userid]);
        sql = `SELECT users.id,users.avatarUrl,users.nickName,users.language comment FROM users JOIN
        (SELECT user_id FROM follow lf WHERE state=1 AND other_user_id=? AND EXISTS(
        SELECT other_user_id FROM follow rf WHERE state=1 AND user_id=? AND lf.user_id=rf.other_user_id)) f ON users.id=f.user_id`;
        // 已同意的通知
        let myagree = await sqlQuery(sql, [userid, userid]);
        sql = `SELECT users.id,users.avatarUrl,users.nickName,users.language comment FROM users JOIN
        (SELECT user_id FROM follow WHERE state=0 AND other_user_id=?) f ON users.id=f.user_id`;
        let mydisagree = await sqlQuery(sql, [userid]);
        ctx.body = {
            state: 200,
            message: {
                other,
                my,
                myagree,
                mydisagree
            }
        }
    } catch (error) {
        next(error)
    }
})
// 同意好友申请
router.get('/agree', async (ctx, next) => {
    try {
        let { uid, oid } = ctx.query;
        let sql = `INSERT INTO follow(user_id,other_user_id) VALUES(?,?)`;
        let result = await sqlQuery(sql, [uid, oid]);
        ctx.body = {
            state: 200,
            message: result
        }
    } catch (error) {
        next(error)
    }
})
// 拒绝好友申请
router.get('/disagree', async (ctx, next) => {
    try {
        let { uid, oid } = ctx.query;
        let sql = `UPDATE follow SET state=0 WHERE user_id=? AND other_user_id=?`;
        let result = await sqlQuery(sql, [oid, uid]);
        ctx.body = {
            state: 200,
            message: result
        }
    } catch (error) {
        next(error)
    }
})
module.exports = router
